package com.lin.PP;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.lin.Sign.PP;
import com.lin.gobang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by x1 on 2016/5/19.
 */
public class PanelPP extends View {
    private float msize;
    private Paint p=new Paint();
    public static int score;
    int width;
    private boolean iswhite=true;
    private Bitmap black;
    private Bitmap white;
    private List<Point> whiteArray=new ArrayList<>();
    private List<Point>blackArray=new ArrayList<>();
    private boolean isgameover;
    private boolean iswhitewinner;

    public PanelPP(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        p.setColor(0x99000000);
        p.setAntiAlias(true);
        p.setDither(true);
        p.setStyle(Paint.Style.STROKE);
        black= BitmapFactory.decodeResource(getResources(), R.mipmap.blackx);
        white=BitmapFactory.decodeResource(getResources(),R.mipmap.whitey);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthsize=MeasureSpec.getSize(widthMeasureSpec);
        int widthmode=MeasureSpec.getMode(widthMeasureSpec);
        int heightsize=MeasureSpec.getSize(heightMeasureSpec);
        int heightmode=MeasureSpec.getMode(heightMeasureSpec);
        width=Math.min(widthsize,heightsize);
        if(widthmode==MeasureSpec.UNSPECIFIED)
        {
            width=heightsize;
        }
        else if(heightmode==MeasureSpec.UNSPECIFIED){
            width=widthsize;
        }
        setMeasuredDimension(width,width);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        msize=width*1.0f/15;
        int qizi=(int)msize*3/4;
        white=Bitmap.createScaledBitmap(white,qizi,qizi,false);
        black=Bitmap.createScaledBitmap(black,qizi,qizi,false);
    }
    private Point getvalidpoint(int x,int y) {
        int px=(int)(x/msize);
        int py=(int)(y/msize);
        return new Point(px,py);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action=event.getAction();
        if(isgameover)
            return false;
        if(action==MotionEvent.ACTION_UP){
            int x=(int)event.getX();
            int y=(int)event.getY();
            Point p=getvalidpoint(x,y);
            if(blackArray.contains(p)||whiteArray.contains(p))
            {
                return false;
            }
            if(iswhite)
            {
                whiteArray.add(p);
            }
            else {
                blackArray.add(p);
            }
            System.out.println(p);
            invalidate();
            iswhite=!iswhite;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBoard(canvas);
        drawqizi(canvas);
        checkgameover();
    }

    private void checkgameover() {
        boolean whitewin= checkline(whiteArray);
        boolean blackwin=checkline(blackArray);
        if(blackwin||whitewin)
        {
            isgameover=true;
            iswhitewinner=whitewin;
            String text=iswhitewinner?"白棋胜利":"黑棋胜利";
            Toast.makeText(getContext(),text,Toast.LENGTH_SHORT).show();

        }

    }

    private boolean checkline(List<Point> points) {
        for (Point p:points)
        {
            int x= p.x;
            int y= p.y;
            boolean win;
            win=checkHorizon(x,y,points);
            if(win)return true;
            win=checkVertical(x,y,points);
            if(win)return true;
            win=checkLeft(x,y,points);
            if(win)return true;
            win=checkRight(x,y,points);
            if(win)return true;

        }
        return false;
    }

    private boolean checkHorizon(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x+i,y)))
            {
                count++;
            }
            else
                break;
        }
        if(count==5)
            return true;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x-i,y)))
                count++;
            else
                break;
        }
        if(count==5)
            return true;
        return false;
    }
    private boolean checkVertical(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x,y+i)))
            {
                count++;
            }
            else
                break;
        }
        if(count==5)
            return true;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x,y-i)))
                count++;
            else
                break;
        }
        if(count==5)
            return true;
        return false;
    }
    private boolean checkLeft(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x-i,y+i)))
            {
                count++;
            }
            else
                break;
        }
        if(count==5)
            return true;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x+i,y-i)))
                count++;
            else
                break;
        }
        if(count==5)
            return true;
        return false;
    }
    private boolean checkRight(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x+i,y+i)))
            {
                count++;
            }
            else
                break;
        }
        if(count==5)
            return true;
        for(int i=1;i<5;i++)
        {
            if(points.contains(new Point(x-i,y-i)))
                count++;
            else
                break;
        }
        if(count==5)
            return true;
        return false;
    }
    private void drawqizi(Canvas canvas) {
        int n=whiteArray.size();
        for(int i=0;i<n;i++){
            Point whitepoint=whiteArray.get(i);
            canvas.drawBitmap(white,(float)(whitepoint.x+1/8)*msize,(float)(whitepoint.y+1/8)*msize,null);
        }
        n=blackArray.size();
        for(int i=0;i<n;i++){
            Point blackpoint=blackArray.get(i);
            canvas.drawBitmap(black,(float)(blackpoint.x+1/8)*msize,(float)(blackpoint.y+1/8)*msize,null);
        }
    }
    private void drawBoard(Canvas canvas) {
        float size=msize;
        for(int i=0;i<15;i++){
            float startx=size/2;
            float endx=width-size/2;
            float y=(msize/2)+msize*i;
            canvas.drawLine(startx,y,endx,y,p);
            canvas.drawLine(y,startx,y,endx,p);

        }
    }
}
